﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace luanvanFinal
{
    class clsStage
    {
        String title;
        int idProject;
        Nullable<DateTime> startDay = null;
        Nullable<DateTime> endDay = null;
        String description;
        float totalPercent;
        public String Title
        {
            get { return title; }
            set { title = value; }
        }
        public int IdProject
        {
            get { return idProject; }
            set { idProject = value; }
        }
        public Nullable<DateTime> StartDay
        {
            get { return startDay; }
            set { startDay = value; }
        }
        public Nullable<DateTime> EndDay
        {
            get { return endDay; }
            set { endDay = value; }
        }
        public String Description
        {
            get { return description; }
            set { description = value; }
        }
        public float TotalPercent
        {
            get { return totalPercent; }
            set { totalPercent = value; }
        }
        public clsStage()
        {
            title = "";
            idProject = 0;
            startDay = null;
            endDay = null;
            description = "";
            totalPercent = 0;
        }
        public void insertStage()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("insertStage", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idProject", SqlDbType.Int).Value = this.idProject;
                sqlcmd.Parameters.Add("@title", SqlDbType.NVarChar).Value = this.title;
                sqlcmd.Parameters.Add("@startDay", SqlDbType.DateTime).Value = this.startDay;
                sqlcmd.Parameters.Add("@endDay", SqlDbType.DateTime).Value = this.endDay;
                sqlcmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = this.description;
                sqlcmd.Parameters.Add("@totalPercent", SqlDbType.NVarChar).Value = this.totalPercent;      
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
        public void updateStage()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("updateStage", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idProject", SqlDbType.Int).Value = this.idProject;
                sqlcmd.Parameters.Add("@title", SqlDbType.NVarChar).Value = this.title;
                sqlcmd.Parameters.Add("@startDay", SqlDbType.DateTime).Value = this.startDay;
                sqlcmd.Parameters.Add("@endDay", SqlDbType.DateTime).Value = this.endDay;
                sqlcmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = this.description;
                sqlcmd.Parameters.Add("@totalPercent", SqlDbType.Float).Value = this.totalPercent;
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
        public void deleteStage()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("deleteStage", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idProject", SqlDbType.Int).Value = this.idProject;
                sqlcmd.Parameters.Add("@title", SqlDbType.NVarChar).Value = this.title;
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
        public void deleteStageByIdProject()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                
                SqlCommand sqlcmd = new SqlCommand("deleteStageByIdProject", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idProject", SqlDbType.Int).Value = this.idProject;                
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
        public DataTable getDataStage(String strSQL)
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            return dt;
        }

    }
}
